/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

#ifndef DynArray_h
#define DynArray_h

template<class T> class DynArray
{
public:
	T *data;
	int capacity;
	int size;

	DynArray()
	{
		capacity = 16;
		size = 0;
		data = new T[capacity];
	}

	DynArray(int capacity)
	{
		this->capacity = capacity;
		size = 0;
		data = new T[capacity];
	}

	~DynArray() 
	{
		delete data;
	}

	void resize(int size)
	{
		while(size > capacity)
		{
			if(capacity < 16)
				setCapacity(16);
			else
				setCapacity(capacity * 2);
		}
		this->size = size;
	}

	void add(const T &a)
	{
		resize(size + 1);
		data[size - 1] = a;
	}

	T *addPointer(int n) 
	{
		resize(size + n);
		return &data[size - n];
	}

	void setCapacity(int c)
	{
		T *d = new T[c];
		int m = c < capacity ? c : capacity;
		int i;

		for(i = 0; i < m; i++)
			d[i] = data[i];
		delete data;
		data = d;
		capacity = c;
	}

	T &operator[](int index)
	{
		return data[index];
	}
};

#endif
